package com.example.zyh.toolslib.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import com.blankj.utilcode.util.BarUtils;
import com.bumptech.glide.Glide;
import com.yanzhenjie.kalle.Kalle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author ZhangYuhang
 * @describe
 * @date 2019/12/16
 * @updatelog
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected Unbinder mUnbinder;
    protected Context mContext;
    public Activity thisActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(initLayout());
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT);
        BarUtils.setNavBarLightMode(this, true);
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        thisActivity=this;
        initView();
        initData();
        initEvent();
    }

    protected abstract int initLayout();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initEvent();


    @Override
    protected void onDestroy() {
        Kalle.cancel(this);
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && !this.isDestroyed()) {
            Glide.with(this).pauseRequests();
        }
    }


    /**
     * 调用拨号界面
     */
    public void call(String phone) {//String phone
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    /**
     * 记录上次点击按钮的时间
     **/
    private long lastClickTime = 0;
    /**
     * 按钮连续点击最低间隔时间 单位：毫秒
     **/
    public final static int CLICK_TIME = 1200;

    /**
     * 防止多次点击
     */
    public boolean fastDoubleClick() {
        if (System.currentTimeMillis() - lastClickTime <= CLICK_TIME) {
            return true;
        }
        lastClickTime = System.currentTimeMillis();
        return false;
    }

    public static boolean isPhoneNumber(String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber)) {
            return false;
        }
        String reg = "^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(phoneNumber);
        return matcher.matches();
    }

    //邮箱验证
    public static boolean isEmail(String strEmail) {
        String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        if (TextUtils.isEmpty(strPattern)) {
            return false;
        } else {
            return strEmail.matches(strPattern);
        }
    }


}
